<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "system".
 *
 * @property string $version
 * @property string $keywords
 * @property string $author
 * @property string $copyright
 * @property string $create_time
 * @property string $contact
 */
class System extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'system';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['version'], 'required'],
            [['create_time'], 'safe'],
            [['version', 'keywords', 'author', 'copyright', 'contact'], 'string', 'max' => 255],
            [['version'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'version' => 'Version',
            'keywords' => 'Keywords',
            'author' => 'Author',
            'copyright' => 'Copyright',
            'create_time' => 'Create Time',
            'contact' => 'Contact',
        ];
    }

    /**
     * get
     *
     * @return System|array|\yii\db\ActiveRecord|null
     */
    public static function getLastOne()
    {
        $model = self::find()->one();

        if (!$model) {
            $model = new self();
        }

        // white space
        $keywords = [];
        if ($model->keywords) {
            $keywords = explode(',', $model->keywords);
            foreach ($keywords as &$keyword) {
                $keyword = trim($keyword);
            }
        }
        $model->keywords = $keywords;

        // contact
        $model->contact = sprintf('电话(微信同): %s', $model->contact);

        return $model;
    }
}
